import { AnimationFrame, Stage, Vector3 } from "babylon-lib/index";
import { AbstractCharacter, IAbstractCharacterData, IAbstractCharacterOptions } from "./abstract-character";
import { INPUT_TYPE } from "./character-controller/character-controller";
import { CharacterHeadLabel } from "./components/character-head-label";


interface ICharacterOptions extends IAbstractCharacterOptions {
    idleIndex?: number;
    walkIndex?: number;
    headLabelOffset?: Vector3;
    runIndex?: number;
    jumpIndex?: number;
}

interface ICharacterData extends IAbstractCharacterData { }


class Character extends AbstractCharacter {

    private _headLabel!: CharacterHeadLabel;

    constructor(stage: Stage, options?: ICharacterOptions) {
        super(stage, options as IAbstractCharacterOptions);
    }

    public get idleIndex(): number | undefined {
        return (this.options as ICharacterOptions).idleIndex;
    }

    public get walkIndex(): number | undefined {
        return (this.options as ICharacterOptions).walkIndex;
    }

    public get runIndex(): number | undefined {
        return (this.options as ICharacterOptions).runIndex;
    }

    public get jumpIndex(): number | undefined {
        return (this.options as ICharacterOptions).jumpIndex;
    }

    protected onInputChange(type: INPUT_TYPE): void {
        // console.log('play action', type)
        switch (type) {
            case INPUT_TYPE.IDLE:
                this.playAction(this.idleIndex!, true);
                break;
            case INPUT_TYPE.WALK:
                this.playAction(this.walkIndex!, true);
                break;
            case INPUT_TYPE.RUN:
                this.playAction(this.runIndex!, true);
                break;
            case INPUT_TYPE.JUMP:
                this.playAction(this.jumpIndex!, false);
                break;
        }
    }

    protected override async initialize(): Promise<void> {
        await super.initialize();
        this.initializeNameLabel();
    }

    private initializeNameLabel(): void {
        const options = this.options as ICharacterOptions;
        const headLabel = new CharacterHeadLabel(this.stage, {
            name: this.name,
        });
        headLabel.container.parent = this.container;
        headLabel.container.position = options.headLabelOffset || new Vector3(0, 1.9, 0);
        this._headLabel = headLabel;
    }

}


export { Character };
export type {
    ICharacterOptions,
    ICharacterData,
};